﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using hsgsSHXX.Web.Common;
using System.IO;
using System.Configuration;
using System.Xml.Linq;
using hsgsSHXX.Web.webservice;

namespace hsgsSHXX.Controllers
{
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class HSIOController : Controller
    {
        //
        // GET: /HSIO/

        public ActionResult Index(string gmsfhm)
        {
            if (!string.IsNullOrEmpty(gmsfhm))
            {
                gmsfhm = null != gmsfhm ? Fetch.estimateString(gmsfhm, CheckGetEnum.Safety) : string.Empty;
                string strWhere = "hh=(select top 1 hh from hsczrk where gmsfhm='" + gmsfhm + "' and zxbz=0) and zxbz=0";
                BLL.hsczrk hsczrkBLL = new BLL.hsczrk();
                List<Model.hsczrk> hsczrkList = hsczrkBLL.GetModelListByCard(strWhere);
                //foreach(var item in hsczrkList)
                //{
                //    GetLocalCZRK(item);
                //}
                if (hsczrkList.Count == 0)
                {

                    GetLocalCZRK(gmsfhm);
                }
                else
                {
                    GetLocalCZRK(hsczrkList);
                }

            }

            return View();
        }
        /// <summary>
        /// 调用本地常口库的人员信息
        /// </summary>
        /// <param name="hsczrkList">人员列表</param>
        public void GetLocalCZRK(List<Model.hsczrk> hsczrkList)
        {
            //string jsAlert = "调用成功";
            string NBID = ConfigurationManager.AppSettings["NBID"].ToString();
            string NBGApwd = ConfigurationManager.AppSettings["NBGApwd"].ToString();

            string strWhere = "";
            List<string> strList = new List<string>();
            BLL.hsczrk_dysj hsczrk_dysjBLL = new BLL.hsczrk_dysj();
            List<Model.hsczrk_dysj> hsczrk_dysjList = new List<Model.hsczrk_dysj>();
            foreach (var hsczrkModel in hsczrkList)
            {
                strWhere = "";
                if (hsczrkModel.GMSFHM != "")
                {
                    string xmlStr = "";
                    NBGASVC nbgasvcService = null;
                    Model.hsczrk_dysj hsczrk_dysjModel = new Model.hsczrk_dysj();
                    if (ApplicationSettings.Get("DevModel") == "true")
                    {
                        //开发模式
                        xmlStr = @"<returnxml><returncode>200</returncode><returnstr>接口运行正常</returnstr><data><detail><CZRKID>3302000000030369208</CZRKID><CZRKGMSFHM>330283198202170046</CZRKGMSFHM><CZRKXM>徐琳</CZRKXM><CZRKCYM></CZRKCYM><CZRKXB>2</CZRKXB><CZRKMZ>01</CZRKMZ><CZRKCSRQ>19820217</CZRKCSRQ><CZRKCSDGJ></CZRKCSDGJ><CZRKCSDSSX>330283</CZRKCSDSSX><JLX>330203001492</JLX><CZRKCSDXZ></CZRKCSDXZ><CZRKJGGJ></CZRKJGGJ><CZRKJGSSX>330283</CZRKJGSSX><CZRKWHCD>20</CZRKWHCD><CZRKHYZK>10</CZRKHYZK><CZRKBYZK>0</CZRKBYZK><CZRKSG>160</CZRKSG><CZRKZY>金融</CZRKZY><CZRKFWCS>工商银行宁波市东门支行</CZRKFWCS><CZRKSSSSXQ>330203</CZRKSSSSXQ><CZRKZZ>４３号</CZRKZZ><CZRKXXJB>5</CZRKXXJB><CZRKZXBS> </CZRKZXBS><CZRKZXRQ> </CZRKZXRQ><CZRKPCSMC> </CZRKPCSMC><CZRKPCSJGDM>330203025</CZRKPCSJGDM><CZRKXJGAJGMC> </CZRKXJGAJGMC><CZRKXJGAJGJGDM> </CZRKXJGAJGJGDM><CZRKFQID> </CZRKFQID><CZRKXPID>3302000001004843424</CZRKXPID><CZRKRYZT>0</CZRKRYZT><CZRKHH>3302000000030075653</CZRKHH><CZRKJTGX>99</CZRKJTGX><CZRKDHHM>13586880250</CZRKDHHM><TB_XLBH>24413648</TB_XLBH><CZRKQFJG>宁波市公安局海曙分局</CZRKQFJG><CZRKYXQXQSRQ>20071017</CZRKYXQXQSRQ><CZRKYXQXJZRQ>20171017</CZRKYXQXJZRQ><CZRKDSHH>025013836</CZRKDSHH><JWH>330203025005</JWH><RYNBID>3302000000030369208</RYNBID><TB_RKSJ>2011-10-15 09:45:24.0</TB_RKSJ></detail></data></returnxml>";
                    }
                    else
                    {
                        //部署模式
                        nbgasvcService = new NBGASVC();
                        try
                        {
                            xmlStr = nbgasvcService.nbgaServices(NBID, NBGApwd, new object[1] { hsczrkModel.GMSFHM });//调用接口

                        }
                        catch
                        {
                            //jsAlert = "调用失败";
                            Response.Write("调用失败");
                            Response.Flush();
                            Response.End();
                        }

                    }
                    if (xmlStr != "")
                    {

                        XElement xmlTree = XElement.Parse(xmlStr);
                        XElement childNodes = null;
                        string returncode = xmlTree.Element("returncode").Value;
                        if (returncode != "200")
                        {
                            //jsAlert = "调用失败";
                            Response.Write("调用失败");
                            Response.Flush();
                            Response.End();
                            //break;
                        }
                        //else if (returncode == "104")
                        //{
                        //    jsAlert = "IP不在访问范围内";
                        //}
                        //else if (returncode == "103")
                        //{
                        //    jsAlert = "服务已经停用";
                        //}

                        if (returncode == "200")
                        {
                            xmlTree = xmlTree.Element("data");

                            childNodes = xmlTree.Element("detail").Element("CZRKXM");
                            hsczrk_dysjModel.CZRKXM = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.XM)
                            {
                                strWhere += "XM='" + childNodes.Value + "',";
                            }
                            childNodes = xmlTree.Element("detail").Element("CZRKCYM");
                            hsczrk_dysjModel.CZRKCYM = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.BM)
                            {
                                strWhere += "BM='" + childNodes.Value + "',";
                            }
                            childNodes = xmlTree.Element("detail").Element("CZRKXB");
                            hsczrk_dysjModel.CZRKXB = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.XB)
                            {
                                strWhere += "XB='" + childNodes.Value + "',";
                            }
                            childNodes = xmlTree.Element("detail").Element("CZRKMZ");
                            hsczrk_dysjModel.CZRKMZ = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.MZ)
                            {
                                strWhere += "MZ='" + childNodes.Value + "',";
                            }
                            childNodes = xmlTree.Element("detail").Element("CZRKCSRQ");
                            hsczrk_dysjModel.CZRKCSRQ = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.CSRQ)
                            {
                                strWhere += "CSRQ='" + childNodes.Value + "',";
                            }
                            childNodes = xmlTree.Element("detail").Element("CZRKCSDGJ");
                            hsczrk_dysjModel.CZRKCSDGJ = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.CSDGJ)
                            {
                                strWhere += "CSDGJ='" + childNodes.Value + "',";
                            }
                            childNodes = xmlTree.Element("detail").Element("CZRKCSDSSX");
                            hsczrk_dysjModel.CZRKCSDSSX = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.CSDSSX)
                            {
                                strWhere += "CSDSSX='" + childNodes.Value + "',";
                            }
                            childNodes = xmlTree.Element("detail").Element("JLX");
                            hsczrk_dysjModel.JLX = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.JLX)
                            {
                                strWhere += "JLX='" + childNodes.Value + "',";
                            }
                            childNodes = xmlTree.Element("detail").Element("CZRKCSDXZ");
                            hsczrk_dysjModel.CZRKCSDXZ = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.CSDXZ)
                            {
                                strWhere += "CSDXZ='" + childNodes.Value + "',";
                            }
                            childNodes = xmlTree.Element("detail").Element("CZRKJGGJ");
                            hsczrk_dysjModel.CZRKJGGJ = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.JGGJ)
                            {
                                strWhere += "JGGJ='" + childNodes.Value + "',";
                            }
                            childNodes = xmlTree.Element("detail").Element("CZRKJGSSX");
                            hsczrk_dysjModel.CZRKJGSSX = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.JGSSX)
                            {
                                strWhere += "JGSSX='" + childNodes.Value + "',";
                            }
                            childNodes = xmlTree.Element("detail").Element("CZRKWHCD");
                            hsczrk_dysjModel.CZRKWHCD = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.WHCD)
                            {
                                strWhere += "WHCD='" + childNodes.Value + "',";
                            }
                            childNodes = xmlTree.Element("detail").Element("CZRKHYZK");
                            hsczrk_dysjModel.CZRKHYZK = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.HYZK)
                            {
                                strWhere += "HYZK='" + childNodes.Value + "',";
                            }
                            childNodes = xmlTree.Element("detail").Element("CZRKBYZK");
                            hsczrk_dysjModel.CZRKBYZK = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.BYZK)
                            {
                                strWhere += "BYZK='" + childNodes.Value + "',";
                            }
                            childNodes = xmlTree.Element("detail").Element("CZRKSG");
                            hsczrk_dysjModel.CZRKSG = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.SG)
                            {
                                strWhere += "SG='" + childNodes.Value + "',";
                            }
                            childNodes = xmlTree.Element("detail").Element("CZRKZY");
                            hsczrk_dysjModel.CZRKZY = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.ZY)
                            {
                                strWhere += "ZY='" + childNodes.Value + "',";
                            }
                            childNodes = xmlTree.Element("detail").Element("CZRKFWCS");
                            hsczrk_dysjModel.CZRKFWCS = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.FWCS)
                            {
                                strWhere += "FWCS='" + childNodes.Value + "',";
                            }
                            childNodes = xmlTree.Element("detail").Element("CZRKSSSSXQ");
                            hsczrk_dysjModel.CZRKSSSSXQ = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.CZRKSSSSXQ)
                            {
                                strWhere += "CZRKSSSSXQ='" + childNodes.Value + "',";
                            }
                            childNodes = xmlTree.Element("detail").Element("CZRKZZ");
                            hsczrk_dysjModel.CZRKZZ = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.CZRKZZ)
                            {
                                strWhere += "CZRKZZ='" + childNodes.Value + "',";
                            }
                            childNodes = xmlTree.Element("detail").Element("CZRKHH");
                            hsczrk_dysjModel.CZRKHH = childNodes.Value;
                            //if (childNodes.Value != hsczrkModel.HH)
                            //{
                            //    strWhere += "HH='" + childNodes.Value + "',";
                            //}
                            childNodes = xmlTree.Element("detail").Element("CZRKJTGX");
                            hsczrk_dysjModel.CZRKJTGX = childNodes.Value;
                            if (childNodes.Value != hsczrkModel.YHZGX)
                            {
                                strWhere += "YHZGX='" + childNodes.Value + "',";
                            }
                            strWhere = strWhere.TrimEnd(',');
                            if (strWhere != "")
                            {
                                strList.Add(strWhere + " where GMSFHM='" + hsczrkModel.GMSFHM + "' and zxbz=0;");
                            }
                            hsczrk_dysjModel.GMSFHM = hsczrkModel.GMSFHM;
                            hsczrk_dysjList.Add(hsczrk_dysjModel);
                        }

                    }

                }
                else
                {
                    //context.Response.Write(_jsonstr);
                }
            }
            if (strList.Count > 0)
            {
                BLL.hsczrk hsczrkBLL = new BLL.hsczrk();
                try
                {
                    hsczrk_dysjBLL.AddList(hsczrk_dysjList);
                    hsczrkBLL.UpdateHsczrk(strList);
                    Response.Write("调用成功");
                    Response.Flush();
                    Response.End();
                    //jsAlert = "调用成功";
                }
                catch
                {
                    //jsAlert = "调用失败";
                    Response.Write("调用失败");
                    Response.Flush();
                    Response.End();
                }
            }

            //ViewData["jsAlert"] = jsAlert;

        }

        /// <summary>
        /// 调用本地常口库的人员信息
        /// </summary>
        /// <param name="gmsfhm">身份证号</param>
        public void GetLocalCZRK(string gmsfhm)
        {
            //string jsAlert = "调用成功";
            string NBID = ConfigurationManager.AppSettings["NBID"].ToString();
            string NBGApwd = ConfigurationManager.AppSettings["NBGApwd"].ToString();
            BLL.hsczrk_dysj hsczrk_dysjBLL = new BLL.hsczrk_dysj();
            if (gmsfhm != "")
            {
                string xmlStr = "";
                NBGASVC nbgasvcService = null;
                Model.hsczrk_dysj hsczrk_dysjModel = new Model.hsczrk_dysj();
                if (ApplicationSettings.Get("DevModel") == "true")
                {
                    //开发模式
                    xmlStr = @"<returnxml>
                                      <returncode>200</returncode>
                                      <returnstr>接口运行正常</returnstr>
                                      <data>
                                        <detail>
                                          <CZRKID>3302000000030369208</CZRKID>
                                          <CZRKGMSFHM>330283198202170046</CZRKGMSFHM>
                                          <CZRKXM>徐琳</CZRKXM>
                                          <CZRKCYM></CZRKCYM>
                                          <CZRKXB>2</CZRKXB>
                                          <CZRKMZ>01</CZRKMZ>
                                          <CZRKCSRQ>19820217</CZRKCSRQ>
                                          <CZRKCSDGJ></CZRKCSDGJ>
                                          <CZRKCSDSSX>330283</CZRKCSDSSX>
                                          <JLX>330203001492</JLX>
                                          <CZRKCSDXZ></CZRKCSDXZ>
                                          <CZRKJGGJ></CZRKJGGJ>
                                          <CZRKJGSSX>330283</CZRKJGSSX>
                                          <CZRKWHCD>20</CZRKWHCD>
                                          <CZRKHYZK>10</CZRKHYZK>
                                          <CZRKBYZK>0</CZRKBYZK>
                                          <CZRKSG>160</CZRKSG>
                                          <CZRKZY>金融</CZRKZY>
                                          <CZRKFWCS>工商银行宁波市东门支行</CZRKFWCS>
                                          <CZRKSSSSXQ>330203</CZRKSSSSXQ>
                                          <CZRKZZ>４３号</CZRKZZ>
                                          <CZRKXXJB>5</CZRKXXJB>
                                          <CZRKZXBS></CZRKZXBS>
                                          <CZRKZXRQ></CZRKZXRQ>
                                          <CZRKPCSMC></CZRKPCSMC>
                                          <CZRKPCSJGDM>330203025</CZRKPCSJGDM>
                                          <CZRKXJGAJGMC></CZRKXJGAJGMC>
                                          <CZRKXJGAJGJGDM></CZRKXJGAJGJGDM>
                                          <CZRKFQID></CZRKFQID>
                                          <CZRKXPID>3302000001004843424</CZRKXPID>
                                          <CZRKRYZT>0</CZRKRYZT>
                                          <CZRKHH>3302000000030075653</CZRKHH>
                                          <CZRKJTGX>99</CZRKJTGX>
                                          <CZRKDHHM>13586880250</CZRKDHHM>
                                          <TB_XLBH>24413648</TB_XLBH>
                                          <CZRKQFJG>宁波市公安局海曙分局</CZRKQFJG>
                                          <CZRKYXQXQSRQ>20071017</CZRKYXQXQSRQ>
                                          <CZRKYXQXJZRQ>20171017</CZRKYXQXJZRQ>
                                          <CZRKDSHH>025013836</CZRKDSHH>
                                          <JWH>330203025005</JWH>
                                          <RYNBID>3302000000030369208</RYNBID>
                                          <TB_RKSJ>2011-10-15 09:45:24.0</TB_RKSJ>
                                        </detail>
                                      </data>
                                    </returnxml>";


                }
                else
                {
                    //部署模式
                    nbgasvcService = new NBGASVC();
                    try
                    {
                        xmlStr = nbgasvcService.nbgaServices(NBID, NBGApwd, new object[1] { gmsfhm });//调用接口

                    }
                    catch
                    {
                        //jsAlert = "调用失败";
                        Response.Write("调用失败");
                        Response.Flush();
                        Response.End();
                    }

                }
                if (xmlStr != "")
                {

                    XElement xmlTree = XElement.Parse(xmlStr);
                    XElement childNodes = null;
                    string returncode = xmlTree.Element("returncode").Value;
                    if (returncode != "200")
                    {
                        //jsAlert = "调用失败";
                        Response.Write("调用失败");
                        Response.Flush();
                        Response.End();
                        //break;
                    }
                    //else if (returncode == "104")
                    //{
                    //    jsAlert = "IP不在访问范围内";
                    //}
                    //else if (returncode == "103")
                    //{
                    //    jsAlert = "服务已经停用";
                    //}

                    if (returncode == "200")
                    {
                        xmlTree = xmlTree.Element("data");

                        childNodes = xmlTree.Element("detail").Element("CZRKXM");
                        hsczrk_dysjModel.CZRKXM = childNodes.Value;

                        childNodes = xmlTree.Element("detail").Element("CZRKCYM");
                        hsczrk_dysjModel.CZRKCYM = childNodes.Value;

                        childNodes = xmlTree.Element("detail").Element("CZRKXB");
                        hsczrk_dysjModel.CZRKXB = childNodes.Value;

                        childNodes = xmlTree.Element("detail").Element("CZRKMZ");
                        hsczrk_dysjModel.CZRKMZ = childNodes.Value;

                        childNodes = xmlTree.Element("detail").Element("CZRKCSRQ");
                        hsczrk_dysjModel.CZRKCSRQ = childNodes.Value;

                        childNodes = xmlTree.Element("detail").Element("CZRKCSDGJ");
                        hsczrk_dysjModel.CZRKCSDGJ = childNodes.Value;

                        childNodes = xmlTree.Element("detail").Element("CZRKCSDSSX");
                        hsczrk_dysjModel.CZRKCSDSSX = childNodes.Value;

                        childNodes = xmlTree.Element("detail").Element("JLX");
                        hsczrk_dysjModel.JLX = childNodes.Value;

                        childNodes = xmlTree.Element("detail").Element("CZRKCSDXZ");
                        hsczrk_dysjModel.CZRKCSDXZ = childNodes.Value;

                        childNodes = xmlTree.Element("detail").Element("CZRKJGGJ");
                        hsczrk_dysjModel.CZRKJGGJ = childNodes.Value;

                        childNodes = xmlTree.Element("detail").Element("CZRKJGSSX");
                        hsczrk_dysjModel.CZRKJGSSX = childNodes.Value;

                        childNodes = xmlTree.Element("detail").Element("CZRKWHCD");
                        hsczrk_dysjModel.CZRKWHCD = childNodes.Value;

                        childNodes = xmlTree.Element("detail").Element("CZRKHYZK");
                        hsczrk_dysjModel.CZRKHYZK = childNodes.Value;

                        childNodes = xmlTree.Element("detail").Element("CZRKBYZK");
                        hsczrk_dysjModel.CZRKBYZK = childNodes.Value;

                        childNodes = xmlTree.Element("detail").Element("CZRKSG");
                        hsczrk_dysjModel.CZRKSG = childNodes.Value;

                        childNodes = xmlTree.Element("detail").Element("CZRKZY");
                        hsczrk_dysjModel.CZRKZY = childNodes.Value;

                        childNodes = xmlTree.Element("detail").Element("CZRKFWCS");
                        hsczrk_dysjModel.CZRKFWCS = childNodes.Value;

                        childNodes = xmlTree.Element("detail").Element("CZRKSSSSXQ");
                        hsczrk_dysjModel.CZRKSSSSXQ = childNodes.Value;

                        childNodes = xmlTree.Element("detail").Element("CZRKZZ");
                        hsczrk_dysjModel.CZRKZZ = childNodes.Value;

                        childNodes = xmlTree.Element("detail").Element("CZRKHH");
                        hsczrk_dysjModel.CZRKHH = childNodes.Value;
                        //if (childNodes.Value != hsczrkModel.HH)
                        //{
                        //    strWhere += "HH='" + childNodes.Value + "',";
                        //}
                        childNodes = xmlTree.Element("detail").Element("CZRKJTGX");
                        hsczrk_dysjModel.CZRKJTGX = childNodes.Value;

                        try
                        {
                            hsczrk_dysjModel.DYRQ = DateTime.Now;
                            hsczrk_dysjModel.GMSFHM = gmsfhm;
                            hsczrk_dysjBLL.Add(hsczrk_dysjModel);
                            Response.Write("调用成功");
                            Response.Flush();
                            Response.End();
                            //jsAlert = "调用成功";
                        }
                        catch
                        {
                            //jsAlert = "调用失败";
                            Response.Write("调用失败");
                            Response.Flush();
                            Response.End();
                        }
                    }

                }

            }

        }

    }
}
